﻿using lBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class KangFuController : ControllerBase,lKliporBll
    {
        public lKliporBll Bll { get; }
        public ILogger<KangFuController> Logger { get; }

        public KangFuController(lKliporBll bll,ILogger<KangFuController> logger)
        {
            Bll = bll;
            Logger = logger;
        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IActionResult Dao()
        {
            try//异常处理
            {
                Logger.LogInformation("导出成功");
                var klipoerty = Bll.DaoChu();
                XSSFWorkbook sheets = new XSSFWorkbook();
                var sheet = sheets.CreateSheet("康复理疗表");
                var row0 = sheet.CreateRow(0);
                row0.CreateCell(0).SetCellValue("编号");
                row0.CreateCell(1).SetCellValue("项目名称");
                row0.CreateCell(2).SetCellValue("项目编码");
                row0.CreateCell(3).SetCellValue("佣金(元)");
                row0.CreateCell(4).SetCellValue("服务人数");
                row0.CreateCell(5).SetCellValue("服务时长");
                row0.CreateCell(6).SetCellValue("项目说明");
                row0.CreateCell(7).SetCellValue("状态");

                int num = 1;
                foreach (var tr in klipoerty)
                {
                    var row = sheet.CreateRow(num++);
                    row.CreateCell(0).SetCellValue(tr.Id);
                    row.CreateCell(1).SetCellValue(tr.Kname);
                    row.CreateCell(2).SetCellValue(tr.KBma);
                    row.CreateCell(3).SetCellValue(tr.KMoney);
                    row.CreateCell(4).SetCellValue(tr.Kcount);
                    row.CreateCell(5).SetCellValue(tr.Kdate);
                    row.CreateCell(6).SetCellValue(tr.KMessage);
                    row.CreateCell(7).SetCellValue(tr.Zt);

                    num++;
                }

                byte[] bs = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    sheets.Write(ms);
                    bs = ms.ToArray();
                }
                var chu = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bs, chu, "康复理疗表.xlsx");
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="klipoerty">表名</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> KAdd(Klipoerty klipoerty)
        {
            try//异常处理
            {
                Logger.LogInformation("添加成功");
                return Bll.KAdd(klipoerty);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }

        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="name">项目名称模糊查询</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>通过条件查询进行分页和显示</returns>
        [HttpGet]
        public dynamic KShow(string? name, int index, int size)
        {
            try//异常处理
            {
                Logger.LogInformation("显示成功");
                return Bll.KShow(name, index, size);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过主键id进行逻辑删除</returns>
        [HttpDelete]
        public Task<int> Delete(int id)
        {
            try//异常处理
            {
                Logger.LogInformation("逻辑删除成功");
                return Bll.Delete(id);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }

        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过主键id进行批量修改</returns>
        [HttpGet]
        public Task<int> UpdState(string idstring)
        {
            try//异常处理
            {
                Logger.LogInformation("批量修改状态成功");
                return Bll.UpdState(idstring);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public List<Klipoerty> DaoChu()
        {
            try//异常处理
            {
                Logger.LogInformation("导出成功");
                return Bll.DaoChu();
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }
    }
}
